﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using Clipping.Infrastructure.Interfaces;
using Clipping.Infrastructure.Models;
using System.Web;
using System.Text;

namespace Clipping.SearchEnginePageParsers
{
    public class SogouNewsPageParser : ISearchEnginePageParse
    {
		private Encoding _encoding = Encoding.GetEncoding("gb2312");
        public void Parse(string html, out List<Infrastructure.Models.News> news, out string nextPageUrl)
        {
            news = new List<News>();
            nextPageUrl = null;
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            var liNav = doc.CreateNavigator();

            var divSearchResult = liNav.Select("//div[@class='rb']");

            while (divSearchResult.MoveNext())
            {
                News n = new News();

                var li = divSearchResult.Current;
                //取标题和链接
                var link = li.SelectSingleNode("descendant::h3/a");
                if (link != null)
                {
                    string title = removeHtmlComments(link.Value);
                    n.Title =System.Web.HttpUtility.HtmlDecode( title);
                    n.Url = link.GetAttribute("href", "");
                }

                //取媒体和时间
                var mediaAndDtNode = li.SelectSingleNode("descendant::h3/cite");
                if (mediaAndDtNode != null)
                {
                    string mediaAndDt = removeHtmlComments(mediaAndDtNode.Value);
                    if (!string.IsNullOrEmpty(mediaAndDt))
                    {
                        mediaAndDt = mediaAndDt.Trim();
                        int spaceIndex = mediaAndDt.IndexOf(" ");
                        if (spaceIndex > 0)
                        {
                            n.Media = mediaAndDt.Substring(0, spaceIndex);
                            n.PubDate = mediaAndDt.Substring(spaceIndex + 1);
                        }
                    }
                }
                //取摘要和相同新闻链接
                var summaryAndSameNewsNode = li.SelectSingleNode("descendant::div[@class='ft']");
                if (summaryAndSameNewsNode != null)
                {
                    string summary = summaryAndSameNewsNode.Value;
                    var sameNewsNode = summaryAndSameNewsNode.SelectSingleNode("a[@class='samenews']");
                    if (sameNewsNode != null)
                    {
                        string sameNewsUrl = sameNewsNode.GetAttribute("href", "");
                        string sameNewsTitle = sameNewsNode.Value;
                        summary = summary.Replace(sameNewsTitle, "");
						n.SameNewsUrl = System.Web.HttpUtility.HtmlDecode(sameNewsUrl);
                    }
                    summary = removeHtmlComments(summary);
                    summary = System.Web.HttpUtility.HtmlDecode(summary).Trim();
                   
                    n.Summary = summary;
                }
                news.Add(n);

            }
            //取下一页地址
            var nextPageNode = liNav.SelectSingleNode("//a[@id='sogou_next']");
            if (nextPageNode != null && nextPageNode.Value != null && nextPageNode.Value.Contains("下一页"))
            {
                nextPageUrl = nextPageNode.GetAttribute("href", "");
				MatchEvaluator me = new MatchEvaluator(regexReplace);//搜狗的搜索结果中，下一页的地址，对中文字符未编码
				nextPageUrl = Regex.Replace(nextPageUrl, @"\?query=([^&]+)", me);
            }
        }

		private string regexReplace(Match m)
		{

			return @"?query=" + HttpUtility.UrlEncode(m.Groups[1].Value, this._encoding);
		}
        private string removeHtmlComments(string html)
        {
            if (string.IsNullOrEmpty(html))
                return html;
            else 
            {

              return  Regex.Replace(html, @"<!--[\s\S]*?-->", "");
            }
        
        }
    }
}
